package com.zhongxi.guoyuan.fragment.RewardCoin;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.coinShop.SameRewardGoodAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.PostRewardCoinDetailData;
import com.zhongxi.guoyuan.entity.coinStore.SameStlyeGood;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class RewardCoinDetailFragment extends BaseFragment {

    private String desc;
    private String id;
    private NoScrollGridView gvList;
    private WebView wv;
    private List<PostRewardCoinDetailData> datas;
    private RelativeLayout rl_same_goods;

    public RewardCoinDetailFragment(String id, String desc) {
        this.id = id;
        this.desc = desc;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_reward_coin_detail, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        wv = (WebView) getView().findViewById(R.id.wv);
        gvList = (NoScrollGridView) getView().findViewById(R.id.gvList);
        rl_same_goods = (RelativeLayout) getView().findViewById(R.id.rl_same_goods);
    }

    @Override
    protected void initData() {
        super.initData();
        try {
            if (null != desc && !"".equals(desc)) {
                wv.loadDataWithBaseURL(null, getNewContent(desc), "text/html", "utf-8", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        //同款推荐点击进入详情页
        gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, RewardCoinDetailActivity.class);
                intent.putExtra("gid", datas.get(position).getId());
                startActivity(intent);
                getActivity().finish();
            }
        });
        wv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction()==MotionEvent.ACTION_UP){
                    ((WebView) view).requestDisallowInterceptTouchEvent(true);
                }else {
                    ((WebView) view).requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });
    }

    public void initProjectData(String cateid) {
//        String str = "&gid=" + id + "&cateid=" + cateid;
//        getServer(Url.MALL_SAME_STLYE_GOOD, null, str, false, null, "get");
    }

    private String getNewContent(String htmltext) {
        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }
        return doc.toString();
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            SameStlyeGood sameStlyeGood = gson.fromJson(response, SameStlyeGood.class);
            if (sameStlyeGood.getSafeobj() != null && !"".equals(sameStlyeGood.getSafeobj())) {
                MyApplication.getInstance().setSpValue(sameStlyeGood.getSafeobj());
            }
            if (sameStlyeGood.getData() != null && sameStlyeGood.getData().size() > 0) {
              //  rl_same_goods.setVisibility(View.VISIBLE);
                datas = sameStlyeGood.getData();
                SameRewardGoodAdapter stlyeGoodAdapter = new SameRewardGoodAdapter(context, datas);
                gvList.setAdapter(stlyeGoodAdapter);
            } else {
                rl_same_goods.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        rl_same_goods.setVisibility(View.GONE);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if (result.getCode() == 5000) {
                showShortToast("商品信息不完善");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        wv.removeAllViews();
        wv.destroy();
    }
}
